<?php

/**
 * This is the model class for table "{{settings}}".
 *
 * The followings are the available columns in table '{{settings}}':
 * @property integer $id
 * @property string $site_name
 * @property integer $site_offline
 * @property string $offline_message
 * @property integer $list_limit
 * @property string $language
 * @property integer $url_rewriting
 * @property integer $suffix_to_url
 * @property integer $include_site_name
 * @property string $meta_description
 * @property string $meta_keywords
 * @property integer $show_author_meta
 * @property string $robots
 * @property string $update_date
 * @property string $updated_by
 */
class Settings extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Settings the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{settings}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('site_name', 'required'),
            array('site_offline, list_limit, url_rewriting, suffix_to_url, include_site_name, show_author_meta', 'numerical', 'integerOnly'=>true),
            array('site_name, offline_message, meta_keywords', 'length', 'max'=>255),
            array('language', 'length', 'max'=>25),
            array('meta_description', 'length', 'max'=>500),
            array('robots', 'length', 'max'=>30),
            array('updated_by', 'length', 'max'=>50),
            array('update_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_name, site_offline, offline_message, list_limit, language, url_rewriting, suffix_to_url, include_site_name, meta_description, meta_keywords, show_author_meta, robots, update_date, updated_by', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'site_name' => t('LBL_SITENAME', 'settings'),
            'site_offline' => t('LBL_SITEOFFLINE', 'settings'),
            'offline_message' => t('LBL_OFFLINEMESSAGE', 'settings'),
            'list_limit' => t('LBL_LISTLIMIT', 'settings'),
            'language' => t('LBL_LANGUAGE', 'settings'),
            'url_rewriting' => t('LBL_URLREWRITING', 'settings'),
            'suffix_to_url' => t('LBL_SUFFIXURL', 'settings'),
            'include_site_name' => t('LBL_INCLUDESITENAME', 'settings'),
            'meta_description' => t('LBL_METADESC', 'settings'),
            'meta_keywords' => t('LBL_METAKEYWORDS', 'settings'),
            'show_author_meta' => t('LBL_AUTHORMETATAG', 'settings'),
            'robots' => t('LBL_ROBOTS', 'settings'),
            'update_date' => t('LBL_UPDATE_DATE', 'settings'),
            'updated_by' => t('LBL_UPDATED_BY', 'settings'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('site_name',$this->site_name,true);
        $criteria->compare('site_offline',$this->site_offline);
        $criteria->compare('offline_message',$this->offline_message,true);
        $criteria->compare('list_limit',$this->list_limit);
        $criteria->compare('language',$this->language,true);
        $criteria->compare('url_rewriting',$this->url_rewriting);
        $criteria->compare('suffix_to_url',$this->suffix_to_url);
        $criteria->compare('include_site_name',$this->include_site_name);
        $criteria->compare('meta_description',$this->meta_description,true);
        $criteria->compare('meta_keywords',$this->meta_keywords,true);
        $criteria->compare('show_author_meta',$this->show_author_meta);
        $criteria->compare('robots',$this->robots,true);
        $criteria->compare('update_date',$this->update_date,true);
        $criteria->compare('updated_by',$this->updated_by,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    
    public function beforeSave()
    {
        if (parent::beforeSave())
        {
            $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
            $logedInUser = User::model()->findByPk(Yii::app()->user->id);
            $this->update_date = $current_date;
            $this->updated_by = $logedInUser->username;
            return true;
        }
        else
            return false;
    }
    
    public function getListLimit(){
        return array(
            array('code' => '5', 'value' => '5'),
            array('code' => '10', 'value' => '10'),
            array('code' => '15', 'value' => '15'),
            array('code' => '20', 'value' => '20'),
            array('code' => '30', 'value' => '30'),
            array('code' => '50', 'value' => '50'),
            array('code' => '100', 'value' => '100'));
    }
    
    public function getListInclude(){
        return array(
            array('code' => '0', 'value' => 'No'),
            array('code' => '1', 'value' => 'Before'),
            array('code' => '2', 'value' => 'After'));
    }
}